<?php

namespace app\controller\admin;

use app\base\BaseEncryptController;
use app\model\goods\GoodsModel;
use app\model\goods\GoodsStoreModel;
use think\Request;

/**
 * Goods 商品库存管理
 * Class Goods
 * @package app\controller
 */
class GoodsStore extends BaseEncryptController
{
    private $store_model;

    protected function initialize()
    {
        parent::initialize();
        $this->model = new GoodsModel();
        $this->store_model = new GoodsStoreModel();
    }

    /**
     * 分页列表
     * @param string $status
     * @param string $keyword
     * @param int $page
     * @param int $limit
     * @return \think\response\Json
     */
    public function getPage($status = '', $keyword = '', $page = 1, $limit = 10)
    {
        $field = "a.*,b.name as goods_type_name";
        $data = $this->model->getPageData($status, $keyword, $page, $limit, $field);
        return self::returnMsg(1, [
            'totalCount' => $data['total'],
            'currentPage' => $data['current'],
            'pageList' => $data['dataList']
        ]);
    }

    /**
     * 库存变化日志列表
     * @param string $id
     * @param string $aid
     * @param string $operate
     * @param string $keyword
     * @param int $page
     * @param int $limit
     * @return \think\response\Json
     */
    public function listPage($id = '', $aid = "", $operate = "system", $keyword = '', $page = 1, $limit = 10)
    {
        $field = "s.*,g.goods_title,g.goods_image,g.attribute_names";
        $data = $this->store_model->getListData($id, $operate, $aid, $keyword, $page, $limit, $field);
        return self::returnMsg(1, [
            'totalCount' => $data['total'],
            'currentPage' => $data['current'],
            'pageList' => $data['dataList']
        ]);
    }

    /**
     * 单条信息
     * @param $id
     * @return \think\response\Json
     */
    public function getInfo($id)
    {
        $field = "a.*,b.name as goods_type_name";
        $data = $this->model->getGoodsInfo($id, $field);
        if (!empty($data)) {
            return self::returnMsg(1, $data);
        } else {
            return self::returnMsg(1001);
        }
    }

    /**
     * 保存
     * @param Request $request
     * @return \think\response\Json
     */
    public function onSave(Request $request)
    {
        $formData = $request->param();
        $data['id'] = $formData['id'];
        $data['target_id'] = $formData['target_id'];
        $data['attribute_ids'] = $formData['attribute_ids'];
        $remark = $formData['remark'];
        $change = $formData['change'];
        $data['storage'] = intval($formData['storage']);
        $validate = 'app\validate\GoodsStoreValidate';
        $result = $this->validate($data, $validate);
        if (true !== $result) {
            return self::returnMsg(991, [], $result);
        } else {
            $storage = [
                'storage' => $data['storage'],
                'attribute_ids' => $data['attribute_ids'],
                'target_id' => $data['target_id'],
            ];
            $operate = [
                'operate' => 'store',
                'admin_id' => $this->currentUser['user_id'],
            ];
            $ret = $this->model->changeStore($data['id'], $storage, $change, 0, $operate, $remark);
            if (false !== $ret) {
                return self::returnMsg(1);
            } else {
                return self::returnMsg(0);
            }
        }
    }

    /**
     * 删除
     * @param $id
     * @return \think\response\Json
     */
    public function onDelete($id)
    {
        $flag = $this->model::destroy(['id' => $id]);
        if (false !== $flag) {
            $this->model->delRedis($id);
            $this->model->redis->zRem($this->model->redis_key . ':rank', $id);
            $this->model->addToRecycle($id, 'GoodsModel', '商品管理', $this->currentUser['user_id'], $this->currentUser['user_type'], 'goods');
            return self::returnMsg(1);
        } else {
            return self::returnMsg(1002);
        }
    }
}
